package com.example.mydy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.RewardTask;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class RewardAdapter extends RecyclerView.Adapter<RewardAdapter.RewardViewHolder> {

    private List<RewardTask> rewardTasks;
    private OnRewardClickListener listener;

    public interface OnRewardClickListener {
        void onRewardClick(RewardTask rewardTask);
        void onEditClick(RewardTask rewardTask);
        void onDeleteClick(RewardTask rewardTask);
    }

    public RewardAdapter(List<RewardTask> rewardTasks, OnRewardClickListener listener) {
        this.rewardTasks = rewardTasks;
        this.listener = listener;
    }

    @NonNull
    @Override
    public RewardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reward, parent, false);
        return new RewardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RewardViewHolder holder, int position) {
        RewardTask task = rewardTasks.get(position);
        
        // 设置任务标题和描述
        holder.tvRewardTitle.setText(task.getTitle());
        holder.tvRewardDescription.setText(task.getDescription());
        
        // 设置奖励金额
        holder.tvRewardPrice.setText(String.format(Locale.CHINA, "¥ %.2f", task.getReward()));
        
        // 设置任务时间
        if (task.getDeadline() != null && !task.getDeadline().isEmpty()) {
            holder.tvRewardTime.setText("截止日期: " + formatDate(task.getDeadline()));
        } else {
            holder.tvRewardTime.setText("发布时间: " + formatDate(task.getCreateTime()));
        }
        
        // 设置标签显示
        // 设置热门标签
        if (task.isHot()) {
            holder.tagHot.setVisibility(View.VISIBLE);
        } else {
            holder.tagHot.setVisibility(View.GONE);
        }
        
        // 设置最新标签
        if (task.isNew()) {
            holder.tagNew.setVisibility(View.VISIBLE);
        } else {
            holder.tagNew.setVisibility(View.GONE);
        }
        
        // 设置已接取标签
        if (task.isTaken()) {
            holder.tagTaken.setVisibility(View.VISIBLE);
            holder.btnRewardAction.setText("查看详情");
        } else {
            holder.tagTaken.setVisibility(View.GONE);
            holder.btnRewardAction.setText("接取任务");
        }
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRewardClick(task);
            }
        });
        
        // 设置按钮点击事件
        holder.btnRewardAction.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRewardClick(task);
            }
        });
    }

    @Override
    public int getItemCount() {
        return rewardTasks == null ? 0 : rewardTasks.size();
    }

    public void updateData(List<RewardTask> newData) {
        this.rewardTasks = newData;
        notifyDataSetChanged();
    }

    private String formatDate(String dateString) {
        if (dateString == null || dateString.isEmpty()) {
            return "未知时间";
        }
        
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            return outputFormat.format(inputFormat.parse(dateString));
        } catch (Exception e) {
            return dateString;
        }
    }

    public class RewardViewHolder extends RecyclerView.ViewHolder {
        TextView tvRewardTitle;
        TextView tvRewardDescription;
        TextView tvRewardPrice;
        TextView tvRewardTime;
        TextView tagHot;
        TextView tagNew;
        TextView tagTaken;
        Button btnRewardAction;

        public RewardViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRewardTitle = itemView.findViewById(R.id.tv_reward_title);
            tvRewardDescription = itemView.findViewById(R.id.tv_reward_description);
            tvRewardPrice = itemView.findViewById(R.id.tv_reward_price);
            tvRewardTime = itemView.findViewById(R.id.tv_reward_time);
            tagHot = itemView.findViewById(R.id.tag_hot);
            tagNew = itemView.findViewById(R.id.tag_new);
            tagTaken = itemView.findViewById(R.id.tag_taken);
            btnRewardAction = itemView.findViewById(R.id.btn_reward_action);
        }
    }
} 